﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX;
using EdytorWF.Engine;
using System.Windows.Forms;
using EdytorWF.Objects;
using Docking = WeifenLuo.WinFormsUI.Docking;
using System.Drawing;

namespace EdytorWF 
{
    class EditorView : Docking::DockContent
    {
        Point lastPoint;
        Camera ViewCamera;
        ViewInfo info;
        private Panel View;
        Renderer MainRenderer;

        public EditorView()
        {
            InitializeComponent();

            AllowEndUserDocking = true;
            AutoScaleDimensions = new SizeF(6, 13);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(698, 210);
            Controls.Add(View);
            DockAreas = (Docking.DockAreas)((((Docking.DockAreas.Float
            | Docking::DockAreas.DockTop)
            | Docking::DockAreas.DockBottom)
            | Docking::DockAreas.Document));
            Name = "Editor View";
            ShowHint = Docking.DockState.DockBottom;
            Text = "View";
            TabText = "View";


            MainRenderer = new Renderer();
            ViewCamera = new Camera();
            MainRenderer.Initialize(View, ref ViewCamera);
            info = new ViewInfo(MainRenderer.MyDevice);

            MainRenderer.AddViewInfo(ref info);
            Terrain MyTerrain = new Terrain(null);
            MainRenderer.AddObject(MyTerrain);
        }

        public List<SimpleObject> GetObjectList()
        {
            return MainRenderer.ObjectList;
        }

        public void MoveCamera(float x, float y, float z)
        {
            ViewCamera.Move(x, y, z);
            Repaint();
        }

        public void MoveLight(float x, float y, float z)
        {
            
        }

        public void RotateCamera(float p, float y, float r)
        {
            ViewCamera.Rotate(p, y, r);
            Repaint();
        }

        public void Repaint()
        {
            UpdateCamera();
        }

        private void UpdateCamera()
        {
            info.SetViewInfo(ViewCamera.ToString());

            MainRenderer.UpdateCamera();
            MainRenderer.Render();
        }

        public void SetWireFrame()
        {
            MainRenderer.SetWireFrame();
            Repaint();
        }

//////////////////////////////////////////////////////////////////////////
        public void SetSolid()
        {
            MainRenderer.SetSolid();
            Repaint();
        }

//////////////////////////////////////////////////////////////////////////
        void View_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.W)
            {
                MoveCamera(0, 0, 1);
            }
            else if (e.KeyCode == Keys.S)
            {
                MoveCamera(0, 0, -1);
            }
            else if (e.KeyCode == Keys.A)
            {
                MoveCamera(-1, 0, 0);
            }
            else if (e.KeyCode == Keys.D)
            {
                MoveCamera(1, 0, 0);
            }
            else if (e.KeyCode == Keys.R)
            {
                MoveCamera(0, -1, 0);
            }
            else if (e.KeyCode == Keys.T)
            {
                MoveCamera(0, 1, 0);
            }
            else if (e.KeyCode == Keys.Up)
            {
                MoveLight(1, 0, 0);
            }
            else if (e.KeyCode == Keys.Down)
            {
                MoveLight(-1, 0, 0);
            }
            else if (e.KeyCode == Keys.Right)
            {
                MoveLight(0, 1, 0);
            }
            else if (e.KeyCode == Keys.Left)
            {
                MoveLight(1, -1, 0);
            }
        }

//////////////////////////////////////////////////////////////////////////
        void View_MouseWheel(object sender, MouseEventArgs e)
        {
            MoveCamera(0, 0, e.Delta / 120);
        }

//////////////////////////////////////////////////////////////////////////
        private void View_Paint(object sender, PaintEventArgs e)
        {
            Repaint();
        }

//////////////////////////////////////////////////////////////////////////
        private bool LeftPressed = false;
        private bool RightPressed = false;
        private bool MiddlePressed = false;
        private void View_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                LeftPressed = true;
            }
            if (e.Button == MouseButtons.Right)
            {
                RightPressed = true;
            }
            if (e.Button == MouseButtons.Middle)
            {
                MiddlePressed = true;
            }
            lastPoint = e.Location;
        }

//////////////////////////////////////////////////////////////////////////
        private void View_MouseMove(object sender, MouseEventArgs e)
        {
            float y = e.Location.X - lastPoint.X;
            float x = e.Location.Y - lastPoint.Y;
            lastPoint = e.Location;
            if (RightPressed)
            {
                RotateCamera(x * (-0.1f), y * (-0.1f), 0);
            }
            if (MiddlePressed)
            {
                MoveCamera(y * (-0.1f), x * (0.1f), 0);
            }
        }

//////////////////////////////////////////////////////////////////////////
        private void View_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                LeftPressed = false;
            }
            if (e.Button == MouseButtons.Right)
            {
                RightPressed = false;
            }
            if (e.Button == MouseButtons.Middle)
            {
                MiddlePressed = false;
            }
        }

//////////////////////////////////////////////////////////////////////////
        private void View_MouseEnter(object sender, EventArgs e)
        {
            View.Select();
        }

//////////////////////////////////////////////////////////////////////////
        private void InitializeComponent()
        {
            this.View = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // View
            // 
            this.View.Location = new System.Drawing.Point(1, 0);
            this.View.Name = "View";
            this.View.Size = new System.Drawing.Size(637, 584);
            this.View.TabIndex = 0;
            this.View.Paint += new System.Windows.Forms.PaintEventHandler(this.View_Paint);
            this.View.MouseDown += new System.Windows.Forms.MouseEventHandler(this.View_MouseDown);
            this.View.MouseEnter += new System.EventHandler(this.View_MouseEnter);
            this.View.MouseMove += new System.Windows.Forms.MouseEventHandler(this.View_MouseMove);
            this.View.MouseUp += new System.Windows.Forms.MouseEventHandler(this.View_MouseUp);
            this.View.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.View_MouseWheel);
            this.View.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.View_PreviewKeyDown);
            // 
            // EditorView
            // 
            this.ClientSize = new System.Drawing.Size(638, 583);
            this.Controls.Add(this.View);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.Name = "EditorView";
            this.ResumeLayout(false);

        }
    }
}
